<?php

namespace App\Modules\Apicrzs\Http\Controllers;

use App\Traits\CryptAES;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;

use Illuminate\Http\Request;
use App\Traits\Json;
class BaseApicrzsController extends Controller
{

    use Json;

    protected $user;
    protected $user_id;

    protected $service;
    public function __construct(Request $request)
    {
        $token =  $request->header('token');
        $user_info = CryptAES::decryptToken($token,'12');
        if ($user_info){
            $this->user_id = $user_info['data']['user_id'];
        }
    }
    /**
     * 新增流程
     *
     * @param $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function createService($request): JsonResponse
    {
        if ($request instanceof FormRequest){
            $request->validated();
        }

        if (!isset($this->service)){
            return $this->sendApiSuccess([], '请先设置Service或者重写方法！');
        }
        $this->service->create($request->all());
        return $this->sendApiSuccess([], $this->service->getError());
    }
    /**
     * 更新流程
     *
     * @param $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateService($request): JsonResponse
    {
        if ($request instanceof FormRequest){
            $request->validated();
        }

        if (!isset($this->service)){
            return $this->sendApiSuccess([], '请先设置Service或者重写方法！');
        }
        $this->service->update($request->all());

        return $this->sendApiSuccess([], $this->service->getError());
    }

    /**
     * 删除
     *
     * @param  \Illuminate\Http\Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request): JsonResponse
    {
        if (!isset($this->service)){
            return $this->sendApiSuccess([], '请先设置Service或者重写方法！');
        }
        $this->service->delete($request->all());

        return $this->sendApiSuccess([], $this->service->getError());
    }

    public function getSelectLists(Request $request)
    {
        $lists = $this->service->getSelectLists($request,$this->user);
        return $this->sendApiSuccess($lists);
    }

}
